package com.strategy.Abstract;

import com.strategy.strategy.UploadStrategy;
import com.strategy.utils.FileUtil;
import io.minio.errors.*;
import org.joda.time.DateTime;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * ClassName:AbstractUploadStrategyImpl
 * Package:com.strategy.Abstract
 * Description
 *
 * @Author:@wenxueshi
 * @Create:2023/7/16 - 21:52
 * @Version:v1.0
 */

/**
 * 文件上传执行抽象类
 */
public abstract class AbstractUploadStrategyImpl implements UploadStrategy {
    @Override
    public String uploadFile(MultipartFile file) {
        try {
            //region 初始化
            initClient();
            String fileRelativePath;
            try {
                fileRelativePath = executeUpload(file);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            //endregion
            //返回图片访问路径url
            return getPublicNetworkAccessUrl(fileRelativePath);
        } catch (Exception e) {
            throw new RuntimeException("文件上传失败");
        }
    }

    /**
     * 初始化客户端
     */
    public abstract void initClient();

    /**
     * 检查文件是否已经存在（文件MD5值唯一）
     *
     * @param fileRelativePath 文件相对路径
     * @return true 已经存在  false 不存在
     */
    public abstract boolean checkFileIsExisted(String fileRelativePath);

    /**
     * 执行上传操作
     *
     * @param file             文件
     * @param
     * @throws IOException io异常信息
     */
    public abstract String executeUpload(MultipartFile file) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException;

    /**
     * 获取公网访问路径
     *
     * @param fileRelativePath 文件相对路径
     * @return 公网访问绝对路径
     */
    public abstract String getPublicNetworkAccessUrl(String fileRelativePath);
}
